/*
 * @# follow.js 2011-12-27 下午06:48:51
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
$(function(){
	
	//创建圈子弹出层显示
	$('#toCreateCircle').click(function(e){
		$('#selectCircle').show();
	})
	
	//创建圈子弹出层隐藏
	$('#cancelCircle').click(function(e){
		$('#selectCircle').hide();
	});
	//创建圈子弹出层隐藏
	$('#closeCircle').click(function(e){
		$('#selectCircle').hide();
	});
	
	var selectedCircles = {};
	//可能喜欢圈子下选定圈圈
	$(".circle-data li").hover(function(){
		$(this).addClass("memberPTbg");},function(){
		$(this).removeClass("memberPTbg");	
		}).toggle(function(){
			$(this).css({"background-color":"#d3d3d3"}).append('<span class="thisadd"></span>');
			selectedCircles[$(this).text()] = true;
		},function(){
			$(this).css({"background-color":""}).children().remove('.thisadd');
			delete selectedCircles[$(this).text()];
	});
	//所有圈子下选定圈圈
	$(".allcircle-data li").hover(function(){
		$(this).addClass("memberPTbg");},function(){
		$(this).removeClass("memberPTbg");	
		}).toggle(function(){
			$(this).css({"background-color":"#d3d3d3"}).append('<span class="thisadd"></span>');
			selectedCircles[$(this).text()] = true;
		},function(){
			$(this).css({"background-color":""}).children().remove('.thisadd');
			delete selectedCircles[$(this).text()];
	});
	
	//创建圈子
	$('#createCircle').click(function(e){
		var circleNames = [];
		$.each(selectedCircles, function(circleName){
			circleNames.push(circleName);
		});
		if(circleNames.length ==0){
			$('#feedback_message').text('请选择圈圈');
			$('#feedback_message').fadeIn('slow').delay(5000).fadeOut('slow');
			return;
		}
		
		var url = window.ctx + '/circles/create?circle='+circleNames.join(',');
		$.post(url,  function(feedback){
			if(feedback.successful){
				$('#selectCircle').hide();
				var lastCircleId = feedback.obj;
				if(lastCircleId != null){
					location.href=window.ctx +'/circles/'+lastCircleId;
				}
			}else{
				$('#feedback_message').text('提交的圈圈出错');
				$('#feedback_message').fadeIn('slow').delay(5000).fadeOut('slow');
			}
		})
	});
	
	//给某个圈子添加成员弹出层显示
	$('#toAdd').click(function(e){
		$('#setFollow').show();
	});
	//给某个圈子添加成员弹出层隐藏
	$('#cancelFollow').click(function(e){
		$('#setFollow').hide();
	});
	//选定成员提交事件
	$('#addFollow').click(function(e){
		
	});
	
	//设置备注框显示
	$('#setRemark').click(function(e){
		alert("暂出弹出层")
	});
	
	////设置所在圈列表遮盖
	$(document).click(function(e) {
		$('#follow_list [data-role="item"]').hide();
	});
	
	//设置所在圈列表显示
	$('#follow_list i[id^="followcircle_"]').click(function(e){
		var $box = $(this).children();
		if ($box.is(":hidden")) {
			$box.show();
		} 
		e.stopPropagation();
	});
	
	//被关注人加入或移出某个圈
	$('#follow_list i[id^="followcircle_"] [data-node="circle"]').click(function(e){
		var circleId = $(this).attr('data-circle');
		var followId = $(this).attr('data-follow');
		var url = window.ctx +'/circles/'+circleId+'/'+followId+'/out';
		if($(this).prop('checked')){
			url = window.ctx +'/circles/'+circleId+'/'+followId+'/join';
		}
		$.post(url, function(feedback){
			if(feedback.successful){
				var circleNameStr = feedback.obj;
				if(circleNameStr.length > 10){
					circleNameStr = circleNameStr.substring(0,10)+'...';
				}
				$('#follow_span_'+followId).empty().append(circleNameStr);
			}else{
				$('#feedback_message').text('设置分组出错');
				$('#feedback_message').fadeIn('slow').delay(5000).fadeOut('slow');
			}
		})
	});
	
})

//取消关注
function removeAttentionFn(followId){
	var url = window.ctx +'/circle/'+followId+'/remove';
	$.post(url, function(feedback){
		if(feedback.successful){
			$('#follow_'+followId).remove();
		}else{
			alert(feedback.message);
		}
	})
}